package com.zh.note.job;

import cn.hutool.core.date.DateUtil;
import org.quartz.*;

/**
 * @Author: zhanghuan
 * @date: 2025/8/27 15:00
 * @description: 定时任务的具体内容
 */
@PersistJobDataAfterExecution // 有状态的job
@DisallowConcurrentExecution  //禁止并发访问同一个job定义
public class MyJob implements Job {
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        JobDetail jobDetail = context.getJobDetail();
        System.out.println("任务名称：" + jobDetail.getKey().getName());
        System.out.println("任务组名：" + jobDetail.getKey().getGroup());
        System.out.println("任务类名：" + jobDetail.getJobClass().getName());
        System.out.println("本次执行时间" + DateUtil.date(context.getFireTime()));

        //记录任务执行次数
        JobDataMap jobDataMap = jobDetail.getJobDataMap();
        Integer count = (Integer) jobDataMap.get("count");
        System.out.println("第" + count + "次执行");
        jobDataMap.put("count", ++count);
    }
}
